package com.locationhistory.utils;

import java.util.Date;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.locationhistory.bean.LocationHistory;
import com.locationhistory.dao.LocationDAO;
import com.locationhistory.dao.impl.LocationDAOImpl;
import com.locationhistory.net.HttpNetService;
import com.locationhistory.parse.JsonParser;

/**
 * 
 * @author getLocation Thread
 *
 */
public class LocationThread implements Runnable {
	
	public static boolean running = true;
	
	private Context context;
	private LocationDAO locationDAO;
	private static LocationManager locManager;
	private static LocationListener locListener;
	private static Location mobileLocation;
	private LocationHistory dbLocation;
	
	/**
	 * constructor context 
	 * 
	 * @param context
	 */
	public LocationThread (Context context) {
		this.context = context; 
	}

	@Override
	public void run() {
		while(true) {
			getLocation();
			String address 	= getAddress();
			locationDAO 	= new LocationDAOImpl(context);
			
			dbLocation = locationDAO.getLocationByAddress(address);
			if (dbLocation == null) {
				// insert new location
				dbLocation = new LocationHistory();
				dbLocation.setLocation(address);
				dbLocation.setUpdatedTime(CommonUtils.convertFromDateToString(new Date(System.currentTimeMillis()), Constants.Date.MMDDYYYYHHMMSS_PATTERN));
				
				locationDAO.saveLocation(dbLocation);
			}
			
			// sleep thread
			try {
				Thread.sleep(Constants.SLEEP_THREAD_TIME);
			} catch (InterruptedException e) {
				Logger.debug(e.toString());
			}
			
		}
		// check address on the database.
	}
	
	
	 /**
     * get current location base one Network and user's location.
     * 
     */
    private void getLocation() {
    	 locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    	 locListener = new LocationListener() {
    	     @Override
    	     public void onStatusChanged(String provider, int status, Bundle extras) {
    	     }

    	     @Override
    	     public void onProviderEnabled(String provider) {
    	     }

    	     @Override
    	     public void onProviderDisabled(String provider) {
    	     }

    	     @Override
    	     public void onLocationChanged(Location location) {
    	    	 mobileLocation = location;
    	     }
    	 };

    	 locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
    } // End get Current Location

    /**
     * get address
     * 
     * @param context
     * @return
     */
	public String getAddress() {
		String currentLocation = "";
		Logger.debug("Location : " + mobileLocation);
		if (mobileLocation != null) {
			locManager.removeUpdates(locListener); // This needs to stop getting the location data and save the battery power.

			String googleMapAPI  = Constants.GOOGLE_MAP_API;
			googleMapAPI  = googleMapAPI + mobileLocation.getLatitude() + "," + mobileLocation.getLongitude() + "&sensor=true";

			currentLocation = JsonParser.parserAddressByContent(HttpNetService.getDataByURL(googleMapAPI, null));
		} else {
			currentLocation = "";
		}

		return currentLocation;
    } // End get Address
}
